package com.foodorder.service;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

/**
 * 邮件服务类
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class EmailService {
    
    private final JavaMailSender mailSender;
    
    /**
     * 发送简单邮件
     */
    public void sendSimpleEmail(String to, String subject, String content) {
        try {
            SimpleMailMessage message = new SimpleMailMessage();
            message.setFrom("noreply@foodorder.com");
            message.setTo(to);
            message.setSubject(subject);
            message.setText(content);
            
            mailSender.send(message);
            log.info("邮件发送成功: {} -> {}", subject, to);
        } catch (Exception e) {
            log.error("邮件发送失败: {} -> {}, 错误: {}", subject, to, e.getMessage());
        }
    }
    
    /**
     * 发送订单状态通知邮件
     */
    public void sendOrderStatusEmail(String userEmail, String orderNumber, String status) {
        String subject = "订单状态更新通知";
        String content = String.format(
            "亲爱的用户，\n\n" +
            "您的订单 %s 状态已更新为：%s\n\n" +
            "感谢您使用我们的服务！\n\n" +
            "美食订餐平台",
            orderNumber, status
        );
        
        sendSimpleEmail(userEmail, subject, content);
    }
    
    /**
     * 发送店铺新品推送邮件
     */
    public void sendNewProductEmail(String userEmail, String storeName, String productName) {
        String subject = "您关注的店铺有新品上架";
        String content = String.format(
            "亲爱的用户，\n\n" +
            "您关注的店铺 %s 有新品上架：%s\n\n" +
            "快来看看吧！\n\n" +
            "美食订餐平台",
            storeName, productName
        );
        
        sendSimpleEmail(userEmail, subject, content);
    }
    
    /**
     * 发送优惠券到期提醒邮件
     */
    public void sendCouponExpiryEmail(String userEmail, String couponName, String expiryDate) {
        String subject = "优惠券即将过期提醒";
        String content = String.format(
            "亲爱的用户，\n\n" +
            "您的优惠券 %s 将于 %s 过期，请及时使用！\n\n" +
            "美食订餐平台",
            couponName, expiryDate
        );
        
        sendSimpleEmail(userEmail, subject, content);
    }
    
    /**
     * 发送注册欢迎邮件
     */
    public void sendWelcomeEmail(String userEmail, String userName) {
        String subject = "欢迎加入美食订餐平台";
        String content = String.format(
            "亲爱的 %s，\n\n" +
            "欢迎加入美食订餐平台！\n\n" +
            "在这里您可以：\n" +
            "- 浏览附近的美食店铺\n" +
            "- 享受便捷的外卖服务\n" +
            "- 获取专属优惠券\n" +
            "- 关注喜爱的店铺\n\n" +
            "祝您用餐愉快！\n\n" +
            "美食订餐平台",
            userName
        );
        
        sendSimpleEmail(userEmail, subject, content);
    }
} 